package yznu_Bean_handle;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import Goods_Utils.Good;
import index_Model.Join;
import yznu_jdbc.JDpoor;

public class joinManager {
	int size=13;
	Connection con = new JDpoor().CreateCon();
	Statement sql = null;
	PreparedStatement pre = null;
	ResultSet rs = null;
	boolean f = false;
	
	public int countPage(){
		int countPage=0;
		try {
			pre = con.prepareStatement("select count(*) total from join_info;");
			rs=pre.executeQuery();
			if(rs.next()){
				int total=rs.getInt("total");
				countPage=total%size==0?total/size:total/size+1;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.out.println("e");
		} finally {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				System.out.println("e");
			}
		}
		
		return countPage;
	}
	
	public ArrayList<Join> selectAll(int currentPage) {
		// TODO Auto-generated method stub
		ArrayList<Join> joins = new ArrayList<Join>();
		
		try {
			sql = con.createStatement();
			rs=sql.executeQuery("select * from join_info limit "+(currentPage-1)*size+","+size);
			while(rs.next()){
				Join join = new Join();
				join.setName(rs.getString("name"));
				join.setCity(rs.getString("city"));
				join.setPhone(rs.getString("phone"));
				join.setEmail(rs.getString("email"));
				joins.add(join);
	    	}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println(e);
		}
		
		return joins;
	}
	
	public ArrayList<Join> search(int currentPage, String tag, String text) {
		// TODO Auto-generated method stub
		ArrayList<Join> joins = new ArrayList<Join>();
		String condition = "select * from join_info where "+tag+" like '%"+text+"%';";
		
		try {
			sql = con.createStatement();
			rs=sql.executeQuery(condition);
			while(rs.next()){
				Join join = new Join();
				join.setName(rs.getString("name"));
				join.setCity(rs.getString("city"));
				join.setPhone(rs.getString("phone"));
				join.setEmail(rs.getString("email"));
				joins.add(join);
	    	}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return joins;
	}
	
	
}
